package com.stonedt.spider.controller;

import com.stonedt.spider.entity.SmartCollectVo;
import com.stonedt.spider.entity.WebsiteVo;
import com.stonedt.spider.service.SmartCollectService;
import com.stonedt.spider.serviceImpl.ApiCallService;
import com.stonedt.spider.util.PlaywrightUtil;
import com.stonedt.spider.util.RedisUtil;
import com.stonedt.spider.util.ResultUtil;
import com.stonedt.spider.util.ReturnHtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 康涵博
 * @ClassName: ReturnUrlController
 * @Time: 2024/4/24 10:17
 * @Description:
 */
@RestController
public class ReturnHtmlController {
    @Autowired
    private SmartCollectService smartCollectService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ApiCallService apiCallService;

    @GetMapping("/returnHtml")
    public String returnHtml(String baseUrl){
        //通过playwright解析传入的url返回html页面
        String html = PlaywrightUtil.getHtml(baseUrl);
        //通过html转换工具类将HTML页面路径转换并返回
        return ReturnHtmlUtil.processHtml(html, baseUrl);
    }

    /**
     * @Author 康涵博
     * @Description 提交任务
     * @Date 14:27 2024/4/25
     * @Param [com.stonedt.spider.entity.SmartCollectVo]
     * @return java.lang.String
     */
    @PostMapping("/addSmartCollect")
    public ResultUtil addSmartCollect(@RequestBody  SmartCollectVo smartCollectVo,HttpServletRequest request){
        //将智能采集数据插入数据库
        WebsiteVo websiteVo = smartCollectService.addSmartCollect(smartCollectVo,request);
        return ResultUtil.ok(websiteVo);
    }

    /**
     * @Author 康涵博
     * @Description 根据id查询以及详情列表
     * @Date 14:28 2024/4/25
     * @Param []
     * @return com.stonedt.spider.entity.SmartCollectVo
     */
    @GetMapping("/querySmartCollectById")
    public ResultUtil querySmartCollectById(Integer id){
        //先查出所有内容返回
        SmartCollectVo smartCollectVo = smartCollectService.querySmartCollectById(id);
        return ResultUtil.ok(smartCollectVo);
    }

    /**
     * @Author 康涵博
     * @Description 根据id修改内容
     * @Date 13:42 2024/4/28
     * @Param
     * @return
     */
    @PostMapping("/updateSmartCollect")
    public ResultUtil  updateSmartCollect(@RequestBody SmartCollectVo smartCollectVo, HttpServletRequest request){

        try {
            smartCollectService.updateSmartCollect(smartCollectVo,request);
            return ResultUtil.ok("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.ok("修改失败");
    }



}
